package com.junzhiit.nengji.cms.web.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.bainaeco.framework.util.AssertUtil;
import com.junzhiit.nengji.cms.service.SystemTypeService;
import com.junzhiit.nengji.cms.web.vo.ResultVO;
import com.junzhiit.nengji.cms.web.vo.SystemTypeEditVO;
import com.junzhiit.nengji.model.SystemType;

import io.swagger.annotations.ApiOperation;

@RestController
@RequestMapping("/system/type")
public class SystemTypeController extends BaseController {
	
	@Autowired
	private SystemTypeService systemService;
	
	@ApiOperation(value = "查询所有分类信息,未分类")
	@RequestMapping(method = RequestMethod.GET)
	public ResultVO findAll() {
		return systemService.findAll();
	}
	
	@ApiOperation(value = "查询所有分类信息,分好类")
	@RequestMapping(value = "/grading",method = RequestMethod.GET)
	public ResultVO findAllGrading() {
		return systemService.findAllGrading();
	}
	
	@ApiOperation(value = "查询所有分类信息的名称,未分类")
	@RequestMapping(value = "/name",method = RequestMethod.GET)
	public ResultVO findAllName() {
		return systemService.findAllName();
	}
	
	@ApiOperation(value = "查询所有分类信息的名称,分好类")
	@RequestMapping(value = "/name/grading",method = RequestMethod.GET)
	public ResultVO findAllGradingName() {
		return systemService.findAllNameGrading();
	}

	@ApiOperation(value = "查询具体某个分类信息")
	@RequestMapping(value = "/{id}", method = RequestMethod.GET)
	public ResultVO findbyid(@PathVariable String id) {
		return systemService.findbyid(id);
	}

	@ApiOperation(value = "增加分类")
	@RequestMapping(method = RequestMethod.POST)
	public ResultVO create(SystemType typeAddVO) {
		ResultVO result = new ResultVO();
		AssertUtil.notBlank(typeAddVO.getParentId(), "ParentId is required");
		AssertUtil.notBlank(typeAddVO.getName(), "Name is required");
		result = systemService.add(typeAddVO);
		return result;
	}

	@ApiOperation(value = "删除分类")
	@RequestMapping(value = "/{id}", method = RequestMethod.PUT)
	public ResultVO delete(@PathVariable String id) {
		return systemService.delete(id);
	}

	@ApiOperation(value = "更新分类")
	@RequestMapping(method = RequestMethod.PUT)
	public ResultVO update(SystemType typeEditVO) {
		return systemService.update(typeEditVO);
	}
	
	@ApiOperation(value="上传分类图片")
	@RequestMapping(value="/img",method=RequestMethod.PUT)
	public ResultVO updateTypeImage(SystemTypeEditVO typeEditVO){
		AssertUtil.notBlank(typeEditVO.getId(), "id is required");
		return systemService.updateTypeImage(typeEditVO);
	}
	
	@ApiOperation(value="删除上传图片")
	@RequestMapping(value="/img",method=RequestMethod.DELETE)
	public ResultVO deleteTypeImage(String id){
		AssertUtil.notBlank(id, "id is required");
		return systemService.deleteTypeImage(id);
	}
	@ApiOperation(value="根据系统分类ID查询所有该分类的商品")
	@RequestMapping(value="/goods/{id}",method=RequestMethod.GET)
	public ResultVO getGoodsByType(@PathVariable String id){
		AssertUtil.notBlank(id, "id is required");
		return systemService.getGoodsByType(id);
	}
}
